package cn.chainco.cts.idaoImpl;

import java.util.List;

import cn.chainco.cts.idao.IIteamDao;
import cn.chainco.cts.jdo.Company;
import cn.chainco.cts.jdo.Item;

public class ItemDaoImpl  extends GenericHibernateDaoImpl<Item, Long> implements IIteamDao{

	/**
	 * 查找公司的委托项目
	 * @param company
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Item> findItemByCompany(Company company  , Integer status){
		String sql = " select * from cts_item where 1=1 and companyId="+company.getId();
		if(status!=null){
			sql += " and status="+status;
		}
		
		List<Item> itemlist = this.getSession().createSQLQuery(sql).addEntity(Item.class).list();
		return itemlist;
	}
	
	/**
	 * 查询委托项目(公司+系统默认)
	 * @param company
	 * @param status
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Item> findItemByAll(Company company  , Integer status){
		String sql = " select * from cts_item where 1=1 and (companyId="+company.getId() + " or type in (1,2,3,4,5))";
		if(status!=null){
			sql += " and status="+status;
		}
		
		sql += " order by type desc";
		List<Item> itemlist = this.getSession().createSQLQuery(sql).addEntity(Item.class).list();
		return itemlist;
	}
	
	
	/**
	 * 查询
	 * @param company 公司
	 * @param type 项目类型("" , 1-8,多个类型[1,2,3,4])
	 * @param likeName 是否模糊查询(null , 0.中文 , 1.英文)
	 * @param status 状态(null-all , 0正常 , 1禁用 )
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Item> findItem(Company company  , String type , String name  , Integer likeTo , String status){
		String sql = " select * from cts_item where 1=1 ";
		if(company !=null){
			sql += " and companyId="+company.getId();
		}
		
		if(type!=null && !"".equals(type)){
			sql += " and type in ("+type+")";
		}
		
		if(name!=null && name!=""){
		    sql += " and (name_zh like '%"+name+"%' or name_en like '%"+name+"%')";
		}
		
		if(status!=null && !"".equals(status)){
			sql += " and status="+status;
		}
		
		sql += " order by type asc";
		
		List<Item> itemlist = this.getSession().createSQLQuery(sql).addEntity(Item.class).list();
		return itemlist;
	}
}
